import axios from 'axios'
const service = axios.create({
  withCredentials: false,
  timeout: 5000,
  baseURL:import.meta.env.VITE_APP_API_BASEURL || ''
})

service.interceptors.request.use(
  (config) => {
    // 处理文件上传
    if (
        config.method === 'post' &&
        (config.headers)['Content-Type'] === 'application/x-www-form-urlencoded'
      ) {
        config.data = qs.stringify(config.data)
      }
      config.headers["Authorization"] = localStorage.getItem('token')
      console.log('config: ', config);
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  (response) => {
    console.log('response: ', response);
    const res = response.data
    if (response.status !== 200) {
      return Promise.reject(res.msg || 'Error')
    } else {
        console.log('res: ', res);
      return res
    }
  },
  (error) => {
    console.log('err' + error)
    return Promise.reject(error.message)
  }
)
export default {
  get(url, config) {
    return service.get(url, config)
  },

  post(url, data, config) {
    return service.post(url, data, config)
  },

  put(url, data, config) {
    return service.put(url, data, config)
  },

  delete(url, config) {
    return service.delete(url, config)
  }
}
